class Solution {
public:
    vector<vector<int>> generateMatrix(int n)
    {
        vector<vector<int>> ans(n, vector<int>(n));
        int x = 1;
        int a = 0, b = n;
        while (x <= n * n)
        {
            for (int i = a; i < b; i++) ans[a][i] = x++;
            for (int i = a + 1; i < b; i++) ans[i][b - 1] = x++;
            for (int i = b - 2; i >= a; i--) ans[b - 1][i] = x++;
            for (int i = b - 2; i > a; i--) ans[i][a] = x++;
            a++, b--;
        }
        return ans;
    }
};